package com.jjwxc.jwjskandriod.framework.utils;

import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字符串
 *
 * @author uu
 */
public class StUtils {

    /**
     * return s == null || s.trim().length() == 0;
     */
    public static boolean isEmpty(String s) {
        return s == null || s.trim().length() == 0;
    }

    /**
     * 去除空字符串
     *
     * @param str
     * @return
     */
    public static String reBlank(String str) {
        return str;
    }

    /**
     * 去除空字符串
     *
     * @param str
     * @return
     */
    public static String oldReBlank(String str) {

        String dest = "";

        if (str != null) {

            Pattern p = Pattern.compile("\\s*|\t|\r|\n");

            Matcher m = p.matcher(str);

            dest = m.replaceAll("");

        }
        return dest;
    }
    /**
     * 检测输入密码是否符合规范
     * 8~16位数字和字母组成
     * 不能是纯数字或纯字母
     */
    public static boolean isPasswordForm(String pwd) {
        if (isEmpty(pwd)) return false;
        String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,16}$";
        return pwd.matches(regex);

    }
    /**
     * 处理金额，并带小数点
     * @param change
     * @return
     */
    public static String percentage(int change) {
        String mPoin;
        int point = change % 100;
        int mDecimal = change / 100;

        if (point <= 9) {
            mPoin = ".0" + point;
        } else {
            mPoin = "." + point;
        }
        return mDecimal + mPoin;

    }

    /**
     * 获取百分比
     * @param numerator 分子
     * @param denominator 分母
     * @return
     */
    public static String percentage(double numerator, double denominator) {
        double change = numerator / denominator;
        DecimalFormat df = new DecimalFormat("0.00%");
        return df.format(change);
    }
}
